<?php
/**
 * Created by .
 * User: luogan
 * Date: 2022/1/17
 */

namespace app\admin\validate;


use app\model\DeptModel;

class DeptValidate extends BaseValidate
{
    protected $rule = [
        'id' => 'require|isPositiveInteger',
        'name' => 'require|max:50|checkDeptName',
        'description' => 'max:255',
        'page' => 'min:1|isPositiveInteger',
        'mobile' => 'mobile',
        'email' => 'email',
        'duty' => 'max:50',
        'status' => 'integer',
        'sort' => 'integer'
    ];

    /**
     * 定义错误信息
     * 格式：'字段名.规则名'    =>    '错误信息'
     *
     * @var array
     */
    protected $message = [
    ];

    protected $scene = [
        'add' => ['name', 'description', 'mobile', 'email', 'duty', 'status', 'sort'],
        'list' => ['page','status'],
        'info' => ['id'],
        'del' => ['id'],
        'edit' => ['id', 'description', 'mobile', 'email', 'duty', 'status', 'sort'],
    ];


    protected function checkDeptName($value, $rule = '', $data = '', $field = '')
    {
        $res = DeptModel::where('name', $value)->count('id');
        if ($res > 0) {
            return $field . '部门名称重名';
        }
        return true;
    }
}
